package com.lc.demo.service.impl;

import com.lc.demo.bean.*;
import com.lc.demo.mapper.AdminMapper;
import com.lc.demo.mapper.SubjectMapper;
import com.lc.demo.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public void addSubject(Subject subject, String classgradeids) {
        //插入subjetc
        subjectMapper.add(subject);
        //查询出Id
        Integer subjectId = subjectMapper.selectIdByName(subject.getName());
        //获取班级 插入关系表
        String[] split = classgradeids.split(",");
        for (int i = 0; i < split.length; i++) {
            subjectMapper.insertGradeCourse(Integer.valueOf(split[i]), subjectId);
        }

        subjectMapper.add(subject);
    }

    @Override
    public List<Subject> getSubjectList(String username) {
        Admin admin = adminMapper.findByUsername(username);
        Integer departmentid = admin.getDepartmentid();
        return subjectMapper.getSubjectList(departmentid);
    }

    @Override
    public void updateSubject(Subject subject) {
        String[] classgradeids1 = subject.getClassgradeids();

        String username = subject.getUsername();
        Admin admin = adminMapper.findByUsername(username);
        Integer departmentid = admin.getDepartmentid();
        String departmentname = admin.getDepartmentname();
        subject.setDepartmentid(departmentid);
        subject.setDepartmentname(departmentname);

        //删除关系表数据
        subjectMapper.deleteGradeSubject(subject.getId());
        //获取班级 插入关系表
        for (int i = 0; i < classgradeids1.length; i++) {
            subjectMapper.insertGradeCourse(Integer.valueOf(classgradeids1[i]), subject.getId());
        }

        subjectMapper.updateSubject(subject);
    }

    @Override
    public void delSubject(Subject subject) {
        subjectMapper.delSubject(subject);
    }

    @Override
    public Subject selectSubjectByName(String subname) {
        return subjectMapper.selectSubjectByName(subname);
    }

    @Override
    public List<Classgrade> findClassListBySubjectId(Integer subjectId) {
        return subjectMapper.findClassListBySubjectId(subjectId);
    }

    @Override
    public List<Student> findStudentListByClassId(Integer classId) {
        return subjectMapper.findStudentListByClassId(classId);
    }

    @Override
    public Teacher findTeacherByUsername(String username) {
        return subjectMapper.findTeacherByUsername(username);
    }


    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<Map<String,Object>> findSubjectListByTeacherId(Integer id) {

        List<Map<String,Object>> subjectList=new ArrayList<>();
        List<Subject> subjectListByTeacherId=  subjectMapper.findSubjectListByTeacherId(id);


        for (Subject subject : subjectListByTeacherId) {
            Map<String,Object> subjectMap=new HashMap<>();
            subjectMap.put("text",subject.getName());
            subjectMap.put("value",subject.getId());

            List<Classgrade> classList = findClassListBySubjectId(subject.getId());
            List<Map<String,Object>> classGradleMapList=new ArrayList<>();
            for (Classgrade classgrade : classList) {
                Map<String,Object> classgradeMap=new HashMap<>();
                classgradeMap.put("text",classgrade.getClassname());
                classgradeMap.put("value",classgrade.getId());


                //学生
                List<Student> studentList = findStudentListByClassId(classgrade.getId());
                List<Map<String,Object>> studentListMap=new ArrayList<>();
                for (Student student : studentList) {
                    Map<String,Object> studentMap=new HashMap<>();
                    studentMap.put("text",student.getName());
                    studentMap.put("value",student.getUsername());
                    studentListMap.add(studentMap);
                }
                classgradeMap.put("children",studentListMap);
                classGradleMapList.add(classgradeMap);
            }

            subjectMap.put("children",classGradleMapList);
            subjectList.add(subjectMap);
        }

        return subjectList;
//        return subjectList;
    }

    @Override
    public Subject subJectById(Integer subId) {
        return subjectMapper.subJectById(subId);
    }

    @Override
    public void addSubjectByUsername(Subject subject) {

        String[] classgradeids = subject.getClassgradeids();
        String username = subject.getUsername();
        Admin admin = adminMapper.findByUsername(username);
        Integer departmentid = admin.getDepartmentid();
        String departmentname = admin.getDepartmentname();
        subject.setDepartmentid(departmentid);
        subject.setDepartmentname(departmentname);
        //插入subjetc
        subjectMapper.add(subject);
        //查询出Id
        Integer subjectId = subjectMapper.selectIdByName(subject.getName());
        //获取班级 插入关系表
        for (int i = 0; i < classgradeids.length; i++) {
            subjectMapper.insertGradeCourse(Integer.valueOf(classgradeids[i]), subject.getId());
        }

    }

}
